package si.unimb.src.wifisms;

import si.unimb.src.wifisms.control.Database;
import si.unimb.src.wifisms.entity.Account;
import si.unimb.src.wifisms.util.IValidator;
import si.unimb.src.wifisms.util.Utility;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author perox
 * Razred s katerim ustvarimo ali uredimo račun uporabnika.
 */
public class EditAccount extends Activity implements OnClickListener, TextWatcher, IValidator {
	
	/**
	 * Način shranjevanja in ID objekta, ki ga urejamo.
	 */
	private boolean editing = false;
	private int id;
	
	private EditText naziv;
	private EditText username;
	private EditText password;
	//private CheckBox rememberPassword;
	private CheckBox saveMessages;
	private Button save;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_account);
        
        naziv = (EditText)findViewById(R.id.NazivEditText);
        naziv.addTextChangedListener(this);
        username = (EditText)findViewById(R.id.UsernameEditText);
        username.addTextChangedListener(this);
        password = (EditText)findViewById(R.id.PasswordEditText);
        password.addTextChangedListener(this);
        //rememberPassword = (CheckBox)findViewById(R.id.ShraniGesloCheckBox);
        saveMessages = (CheckBox)findViewById(R.id.ShranjujPoslanaSporocilaCheckBox);

        save = (Button)findViewById(R.id.ShraniRacunButton);
        save.setOnClickListener(this);
        
        this.id = getIntent().getIntExtra("id", Utility.NO_EXTRAS);
        if (id > Utility.NO_EXTRAS) {
        	/* urejanje računa */
        	this.editing = true;
        	
            Account account = Database.getAccounts(this).get(id);
            
            naziv.setText(account.getDescription());
            username.setText(account.getUsername());
            password.setText(account.getPassword());
            //rememberPassword.setChecked(account.getRememberPassword());
            saveMessages.setChecked(account.getSaveMessages());
                        
            TextView naslov = (TextView)findViewById(R.id.DodajanjeRacunaText);
            naslov.setText("Urejanje računa " + (id + 1));
            
            validate();
        }
	}
	
	@Override
	public void onClick(View v) {

		if(v.equals(save)) { // save Button
			newAccount(this.editing);
		}	
	}
	
	/**
	 * Funkcija shrani novi ali urejen račun v bazo.
	 * @param edit je način shranjevanja
	 */
	public void newAccount(boolean edit) {

		String pswd = password.getText().toString();
		if (pswd.length() < Utility.PASSWORD_MIN_LENGTH) {
			AlertDialog.Builder adb = new AlertDialog.Builder(this);
			adb.setTitle("Napaka");
			adb.setMessage("Geslo mora biti dolgo vsaj " + Utility.PASSWORD_MIN_LENGTH + " znakov.");
			adb.setPositiveButton("OK", null);
			adb.show();
		    
		    return;
		}
		
		Account noviRacun;
		
		// pri urejanju uredimo obstojeci racun (in ne dodamo vedno novega)
		if(edit) {
			
			noviRacun = Database.getAccounts(this).get(this.id);
			
		} else {
			
			noviRacun = new Account();
			
		}
		
		noviRacun.setDescription(naziv.getText().toString());
		noviRacun.setUsername(username.getText().toString());
		noviRacun.setPassword(pswd);
		noviRacun.setSaveMessages(saveMessages.isChecked());
		//noviRacun.setRememberPassword(rememberPassword.isChecked());
		
		if(edit == false) {
			// shranimo novi račun
			Database.getAccounts(this).add(noviRacun);
		}
		
		Database.persist(this);
		finish();
		
	}
	
	@Override
	public void validate() {

		if(naziv.length() > 0 && username.length() > 0 && password.length() > 0) {
			save.setEnabled(true);
		} else {
			save.setEnabled(false);
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		
		/* validacija -> buttons enabled/disabled */
		validate();
		
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		
	}
}
